
function Update () {
	//var newVector3:Vector3 = transform.position;

	//flattenVertexes(newVector3, 10);
}

function OnMouseUp(){
	if(!Input.GetKey("left ctrl") && !Input.GetKey("left alt") ){
		
		var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		var hit : RaycastHit;
		if (Physics.Raycast (ray, hit, 100)) {
		    Debug.DrawLine (ray.origin, hit.point);
		    flattenVertexes(hit.point, 2);
		}
	}
}

function flattenVertexes(point:Vector3, distance:float){
	
	var theGroundMesh:GameObject = GameObject.Find("GroundMayaMesh");
	var mesh : Mesh = theGroundMesh.GetComponent(MeshFilter).mesh;
	
	
	var localTurretPosition:Vector3 = transform.InverseTransformPoint(point);
	
	var vertices : Vector3[] = mesh.vertices;
	
	var vertexesToChange:Array = new Array();
	var totalValue:float = 0;
	
	for (var p : int = 0; p < vertices.Length; p++) {
		if(Vector3.Distance(vertices[p],localTurretPosition) < distance){
			vertexesToChange.push(p);
			totalValue += vertices[p].y;
		}
	}
	
	var newHeightValue:float = totalValue/vertexesToChange.length;
	
	for(var i:int = 0; i < vertexesToChange.length; i++){
		vertices[vertexesToChange[i]].y = newHeightValue;
	}
	theGroundMesh.GetComponent(MeshFilter).mesh.vertices = vertices;
	theGroundMesh.GetComponent(MeshFilter).mesh.RecalculateNormals();
	
}
